package com.mycinema.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mycinema.biz.CategoryBiz;
import com.mycinema.biz.MovieBiz;
import com.mycinema.entity.Category;
import com.mycinema.entity.Movie;

@Controller
public class HomeController {

	@Autowired
	private CategoryBiz categoryBiz;

	@Autowired
	private MovieBiz movieBiz;

	@RequestMapping("/index")
	public String index(Model model) {
		List<Category> categories = categoryBiz.getAll();
		model.addAttribute("categories", categories);
		return "index"; // 页面名字
	}
	@RequestMapping("/nav")
	public String nav(Model model) {
		List<Category> categories = categoryBiz.getAll();
		model.addAttribute("categories", categories);
		return "nav"; // 页面名字
	}

	@RequestMapping("/list")
	public String list(Model model, Integer cid, String title, Integer page) {
		cid=cid==null?0:cid;
		page=page==null?1:page;
		int pageSize = 3;
		int startIndex = (page - 1) * pageSize;
		List<Movie> movies = movieBiz.getMoviePage(cid, title, startIndex,pageSize);
		int rows=movieBiz.getRowsCount(cid, title);
		int pageCount=(int)Math.ceil((double)rows/pageSize);
		model.addAttribute("movies", movies);
		model.addAttribute("totalPages", pageCount);
		model.addAttribute("categories", categoryBiz.getAll());
		model.addAttribute("title", title);
		model.addAttribute("page", page);
		return "movie-list"; // 页面名字
	}
	
	@RequestMapping("/detail")
	public String detail(int id,Model model){
		Movie movie =movieBiz.getById(id);
		model.addAttribute("movie",movie);
		return "detail";
	}
}
